package com.hskn.hss.module.menu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.button.service.ISysButtonService;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.ksection.entity.Ksection;
import com.hskn.hss.module.menu.entity.SysMenu;
import com.hskn.hss.module.menu.mapper.SysMenuMapper;
import com.hskn.hss.module.menu.service.ISysMenuService;
import com.hskn.hss.module.operatedauthor.service.ISysRoleMenuService;
import com.hskn.hss.module.userrole.service.ISysUserRoleService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-04
 */
@Slf4j
@RestController
@RequestMapping("/hss/sysmenu")
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    ISysRoleMenuService sysRoleMenuService;
    @Autowired
    ISysButtonService sysButtonService;

    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_menu", bean = SysMenu.class, beanMapper = SysMenuMapper.class, names = "name")
    @RequestMapping(value = "insertMenu", method = RequestMethod.POST)
    public AjaxResult<Object> insertMenu(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysMenuService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_menu", idKey = "id", bean = SysMenu.class, beanMapper = SysMenuMapper.class, names = "name")
    @RequestMapping(value = "updateMenu", method = RequestMethod.POST)
    public AjaxResult<Object> updateMenu(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysMenuService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_menu", idKey = "id", bean = SysMenu.class, beanMapper = SysMenuMapper.class, names = "name")
    @RequestMapping(value = "deleteMenuById", method = RequestMethod.POST)
    public AjaxResult<Object> deleteMenuById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return sysMenuService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getMenuById", method = RequestMethod.POST)
    public AjaxResult<Object> getMenuById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysMenu sysMenu = new SysMenu();
        try {
            sysMenu = sysMenuService.getInfoById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysMenu);

    }

    @RequestMapping(value = "getMenuList", method = RequestMethod.POST)
    public AjaxResult<Object> getMenuList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject json = sysMenuService.getList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }


    @RequestMapping(value = "getMenuListByUid", method = RequestMethod.POST)
    public List<SysMenu> getMenuListByUid(HttpServletRequest request) throws HssException {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        String uid = params.getString("uid");
        return sysMenuService.getMenuListByUserid(uid);
    }

    @RequestMapping(value = "getTreeList", method = RequestMethod.POST)
    public AjaxResult<Object> getTreeList() {
        return AjaxResult.success(sysMenuService.getTreeList());
    }

}

